package de.argh.codebook.service.mock;

import java.util.ArrayList;
import java.util.Collection;

import javax.inject.Inject;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import de.argh.codebook.model.Coder;
import de.argh.codebook.service.CoderService;

public class UserDetailServiceMock implements UserDetailsService {

  @Inject
  private CoderService coderService;

  @Override
  public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException {
    final Coder coder = coderService.getCoderByPrincipalName(username);
    
    return new UserDetails() {

      /**
       * generated
       */
      private static final long serialVersionUID = -9075153941483461732L;

      @Override
      public boolean isEnabled() {
        return coder != null;
      }

      @Override
      public boolean isCredentialsNonExpired() {
        return coder != null;
      }

      @Override
      public boolean isAccountNonLocked() {
        return coder != null;
      }

      @Override
      public boolean isAccountNonExpired() {
        return coder != null;
      }

      @Override
      public String getUsername() {
        return coder.getName();
      }

      @Override
      public String getPassword() {
        return coder.getName();
      }

      @Override
      public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
        GrantedAuthority isUser = new SimpleGrantedAuthority("ROLE_USER");
        authorities.add(isUser);

        return authorities;
      }
    };
  }

}
